<template>
    <el-container direction="vertical">
    <Header />
    <el-main style="margin-top: 70px;">
      <el-card class="box-card">
        <h2>注册界面</h2>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名：" prop="names">
            <el-input v-model="ruleForm.names"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="rePass">
            <el-input type="password" v-model="ruleForm.rePass" autocomplete="off"></el-input>
          </el-form-item>
          <el-button type="primary" @click="register" style="width: 200px;">注册</el-button>
        </el-form>
      </el-card>
    </el-main>
    <Footer />
  </el-container>
</template>

<script>
import Footer from "../components/Footer.vue";
import Header from "../components/Header.vue";

export default {
  components: {
    Footer,
    Header,
  },
  data() {
    var renames = (rule, value, callback) => {
      if (!value) {
       return callback(new Error('用户名不能为空'));
      } else {
        callback();
      }

    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.rePass !== '') {
          this.$refs.ruleForm.validateField('rePass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: '',
        rePass: '',
        names: ''

      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        rePass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        names: [
          { validator: renames, trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    async register() {
      if (this.ruleForm.pass !== this.ruleForm.rePass) {
        return alert("两次密码不一致")
      }
        this.axios(
        {
          url: "http://localhost:3000/users/regist",
          method: "POST",
          data:{
            uname:this.ruleForm.names,
            upass:this.ruleForm.pass
          }
        }
      ).then((res) => {
        if (res.data.code == 200) {
          //跳转到登录  
          this.$message.success("注册成功！请登录");
          this.$router.push("/login");
        }else{
          alert(res.data.msg);
        }
      }).catch((err) => {
        console.log(err.message);
      })
     
    },
      
    }
    
}
</script>

<style scoped>
.box-card {
  margin: 0 auto;
  width: 480px;
}

.el-form-item label {
  color: #000000;
}

.el-main {
  height: 550px;
  background-color: #ffffff;
  color: #000000;
  text-align: center;
  line-height: 100px;
}

body>.el-container {
  margin-bottom: 40px;
}

.el-button--primary {
  background-color: rgb(0, 0, 0);
  border-color: rgb(0, 0, 0);
}

.el-button--primary:hover {
  border-color: rgb(89, 88, 87);
  background-color: rgb(89, 88, 87);

  color: white;
}


</style>